﻿using System.Configuration;
using System.Diagnostics;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.ServiceRuntime;

namespace Lucene.Linq.Azure
{
    public class AccountHelper
    {
        public static CloudStorageAccount Account(string configurationSettingName, bool hostedService)
        {
            if (hostedService)
            {
                Trace.TraceInformation("Configuring for hosted service");
                CloudStorageAccount.SetConfigurationSettingPublisher(
                    (configName, configSettingPublisher) =>
                    {
                        var connectionString = RoleEnvironment.GetConfigurationSettingValue(configName);
                        configSettingPublisher(connectionString);
                    }
                );
            }
            else
            {
                Trace.TraceInformation("Configuring for non-hosted service");
                CloudStorageAccount.SetConfigurationSettingPublisher(
                    (configName, configSettingPublisher) =>
                    {
                        var connectionString = ConfigurationManager.ConnectionStrings[configName].ConnectionString;
                        configSettingPublisher(connectionString);
                    }
                );
            }

            return CloudStorageAccount.FromConfigurationSetting(configurationSettingName);
        }

        protected static CloudStorageAccount Account(string connectionString)
        {
            return CloudStorageAccount.Parse(connectionString);
        }
    }
}